package walkera.cn.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import com.tencent.mmkv.MMKV;
import com.wk.smarthome.R;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import walkera.cn.WkConfig;
import walkera.cn.bean.SampReponse;
import walkera.cn.customView.colorpicker.ColorPickerDialog;
import walkera.cn.customView.colorpicker.ColorPickerPanelView;
import walkera.cn.http.XyApi;
import walkera.cn.tool.WkLogTool;



public class HomeSmartActivity extends Activity {
    private Context context ;

    private TextView  systemInfoTv ;
    private EditText hostInput ;
    private ColorPickerPanelView currentColorShow ;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_homesmart);
        context = this ;
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);



        initView();
        getSystemInfo();

    }

    private void initView(){
        currentColorShow = findViewById(R.id.currentColorShow) ;
        hostInput = findViewById(R.id.hostInput) ;
        String host=MMKV.defaultMMKV().decodeString( WkConfig.Key_baseUrl, "http://192.168.1.119:8080/"  );
        MMKV.defaultMMKV().encode( WkConfig.Key_baseUrl,  host);
        hostInput.setText(host);

        systemInfoTv = findViewById(R.id.systemInfoTv) ;
        findViewById(R.id.getColorBtn).setOnClickListener(onClickListener);
        findViewById(R.id.openLed).setOnClickListener(onClickListener);
        findViewById(R.id.closeLed).setOnClickListener(onClickListener);
        findViewById(R.id.ledFlicker).setOnClickListener(onClickListener);
        findViewById(R.id.ledBreath).setOnClickListener(onClickListener);
        findViewById(R.id.saveUrl).setOnClickListener(onClickListener);

        int color =  MMKV.defaultMMKV().decodeInt( WkConfig.KEY_ColorSave, Color.RED);
        currentColorShow.setColor(color);
    }



    private ColorPickerDialog.OnColorChangedListener  onColorChangedListener=
            new ColorPickerDialog.OnColorChangedListener() {
                @Override
                public void onColorChanged(int color) {
                    openCloseLed(true ,color);
                    currentColorShow.setColor(color);
                     MMKV.defaultMMKV().encode( WkConfig.KEY_ColorSave, color);

                }
            };


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId()==R.id.getColorBtn){
                int defaultColor = Color.parseColor("#fffae3") ;
                ColorPickerDialog mDialog = new ColorPickerDialog(  context, defaultColor ,onColorChangedListener  );
                mDialog.show();
            }

            /* else if(R.id.baseInfo== v.getId()){
                getSystemInfo();
            }*/

            else if(R.id.openLed== v.getId()){
                int color =  MMKV.defaultMMKV().decodeInt( WkConfig.KEY_ColorSave, Color.RED);
                openCloseLed(true ,color);
            }

            else if(R.id.closeLed== v.getId()){
                openCloseLed(false ,Color.RED);
            }
            else if(R.id.ledFlicker== v.getId()){
                ledFlicker();
            }

            else if(R.id.ledBreath== v.getId()){
                ledBreath();
            }
            else if(R.id.saveUrl== v.getId()){
                MMKV.defaultMMKV().encode( WkConfig.Key_baseUrl,   hostInput.getText().toString());
            }


        }
    };


    private void ledBreath(){
        int color =  MMKV.defaultMMKV().decodeInt( WkConfig.KEY_ColorSave, Color.RED);
        int sleepTime1=100 ;
        int sleepTime2=300;
        XyApi.getInstance(this).ledBreath(new SingleObserver< Void>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onSuccess(  Void  v) {

            }

            @Override
            public void onError(Throwable e) {
            }
        } ,color , sleepTime1 ,sleepTime2);
    }


    private void ledFlicker(){
        int sleepTime =200 ;
        int []colors = {
                0xFF0000,
                0xFF7F00,
                0xFFFF00 ,
                0x00FF00,
                0x00FFFF,
                0x0000FF,
                0x8B00FF ,
                0xFFFFFF
        } ;

        XyApi.getInstance(this).ledFlicker(new SingleObserver< Void>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onSuccess(  Void  v) {

            }

            @Override
            public void onError(Throwable e) {
                //systemInfoTv.setText(e.getMessage()); //ledFlicker
            }
        } ,colors , sleepTime);
    }

    private void openCloseLed(boolean isOpen , int color){
        XyApi.getInstance(this).openCloseLed(new SingleObserver< Void>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onSuccess(  Void  v) {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }
        } ,isOpen , color);
    }



    private void getSystemInfo(){
        XyApi.getInstance(this).getSystemInfo(new SingleObserver<SampReponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                // 在这里可以拦截请求，可以取消发送
            }

            @Override
            public void onSuccess(SampReponse s) {
                systemInfoTv.setText(s.getMsg()); //getSystemInfo
            }

            @Override
            public void onError(Throwable e) {
                WkLogTool.showLog("e---"+e.getMessage());
                systemInfoTv.setText(e.getMessage()); //getSystemInfo
            }
        });
    }




}
